package com.zlx.test;

import com.zlx.entities.Meeting;
import com.zlx.enums.StatusEnum;
import com.zlx.service.MeetingService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 * @Author 周罗校
 * @Description
 * @ClassName MyTask
 * @date 2020.09.25 12:03:54
 * 该版权归周罗校所有，最终解释权以官方为准
 */
public class MyTask extends TimerTask {
    private boolean flag = false;
    private MeetingService meetingService = new MeetingService();
    //定时器
    @Override
    public void run() {
        if (!flag){
            flag = true;
            List<Meeting> allInfoRToUpdateStatus = meetingService.getAllInfoRToUpdateStatus();
            for (Meeting info : allInfoRToUpdateStatus) {
                if (StatusEnum.END.getValue().equals(info.getStatus())){
                    //如果会议已经结束就不会再更新
                    continue;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date endTime = simpleDateFormat.parse(info.getEndTime());
                    Date startTime = simpleDateFormat.parse(info.getStartTime());
                    if (endTime.getTime()<new Date().getTime()){
                        //会议结束
                        meetingService.updateStatusById(StatusEnum.END.getValue(),info.getId());
                    }else if(startTime.getTime()>new Date().getTime()){
                        //会议未开始
                        meetingService.updateStatusById(StatusEnum.NO_START.getValue(),info.getId());
                    }else if(startTime.getTime()<new Date().getTime()&&endTime.getTime()>new Date().getTime()){
                        //会议进行中
                        meetingService.updateStatusById(StatusEnum.BENGING.getValue(),info.getId());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            flag = false;
        }
    }
}
